<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加订单</title>
    {template 'stourtravel/public/public_min_js'}
    {Common::getCss('style.css,style-new.css,base.css,order-manage.css,base_new.css')}
    {Common::getScript("jquery.validate.js,choose.js,jquery.validate.addcheck.js")}
    <script type="text/javascript" src="/res/js/city/jquery.cityselect.js"></script>
    <script>
        window.CURRENCY_SYMBOL="{Currency_Tool::symbol()}";
    </script>
</head>
<body>
<table class="content-tab">
    <tr>
        <td width="119px" class="content-lt-td"  valign="top">
            {template 'stourtravel/public/leftnav'}
            <!--右侧内容区-->
        </td>
        <td valign="top" class="content-rt-td" style="overflow-y: hidden">

            <form method="post" id="frm" name="frm">

                <div class="order-info-container">
                    <div class="order-info-bar"><strong class="bt-bar">订单信息</strong><a href="javascript:;" class="fr btn btn-primary radius mt-2 mr-10" onclick="window.location.reload()">刷新</a></div>
                    <div class="order-info-block">
                        <ul>

                            <li>
                                <strong class="item-hd">预订会员：</strong>
                                <div class="item-bd">
                                    <a href="javascript:;" class="btn btn-primary radius size-S choose-member" title="选择">选择</a>
                                </div>

                            </li>
                            <li>
                                <strong class="item-hd">产品名称：</strong>
                                <div class="item-bd">
                                    <a href="javascript:;" class="btn btn-primary radius size-S choose-product mt-2" title="选择">选择</a>

                                </div>
                            </li>
                            <li>
                                <strong class="item-hd">选择套餐：</strong>
                                <div class="item-bd">
                                    <select name="suitid" class="drop-down wid_300" id="suit_list">
                                    </select>
                                </div>
                            </li>
                            <li>
                                <strong class="item-hd">使用日期：</strong>
                                <div class="item-bd">
                                    <div class="choose-start-date">
                                        <input type="text" class="date-text choose-date" id="usedate" name="usedate" placeholder="选择出发日期"   />
                                        <i class="date-icon"></i>
                                    </div>
                                </div>
                            </li>
                        </ul>
                        <table class="user-info-table mt-5">
                            <thead>
                                <tr>
                                    <td width="20%">预订数量</td>
                                    <td width="15%">单价</td>
                                    <td width="15%">总计</td>
                                    <td>&nbsp;</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>
                                        <span class="amount-opt-wrap">
                                            <a href="javascript:;" class="sub-btn">&ndash;</a>
                                            <input type="text" class="num-text" name="dingnum" id="field_dingnum" maxlength="4" value="1" />
                                            <a href="javascript:;" class="add-btn">&#43;</a>
                                        </span>
                                    </td>
                                    <td><span class="cor_666 adult_price_single_txt">&yen;0</span></td>
                                    <td><span class="color_f60 adult_price_total_txt">&yen;0</span></td>
                                    <td>&nbsp;</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <!-- 订单信息 -->

                <div class="order-info-container">
                    <div class="order-info-bar"><strong class="bt-bar">联系人信息</strong></div>
                    <div class="order-info-block">
                        <ul>
                            <li>
                                <strong class="item-hd">联系人姓名：</strong>
                                <div class="item-bd">
                                    <input type="text" class="default-text" name="linkman"/>
                                    <span class="star-note-ico">&#42;</span>
                                </div>
                            </li>
                            <li>
                                <strong class="item-hd">联系人电话：</strong>
                                <div class="item-bd">
                                    <input type="text" class="default-text" name="linktel" />
                                    <span class="star-note-ico">&#42;</span>
                                </div>
                            </li>
                            <li>
                                <strong class="item-hd">联系人邮箱：</strong>
                                <div class="item-bd">
                                    <input type="text" class="default-text" name="linkemail" />
                                </div>
                            </li>
                            <li>
                                <strong class="item-hd">预订说明：</strong>
                                <div class="item-bd">
                                    <textarea  class="default-textarea" name="remark"></textarea>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
                <!-- 联系人信息 -->

                <div style="display: none" class="order-info-container mailing_address">
                    <div class="order-info-bar"><strong class="bt-bar">邮寄地址</strong></div>
                    <div class="order-info-block">
                        <ul>
                            <li>
                                <strong class="item-hd">所在地区</strong>
                                <div class="item-bd" id="m_area">
                                    <input type="text" placeholder="省/直辖市" class="default-text w100" name="m_area_prov" />
                                    <input type="text" placeholder="市/区" class="default-text w100" name="m_area_city" />
                                </div>
                            </li>
                            <li>
                                <strong class="item-hd">详细地址：</strong>
                                <div class="item-bd">
                                    <textarea class="default-textarea" name="m_address"></textarea>
                                </div>
                            </li>
                            <li>
                                <strong class="item-hd">邮政编码：</strong>
                                <div class="item-bd">
                                    <input type="text" class="default-text" name="m_postcode" />
                                </div>
                            </li>
                            <li>
                                <strong class="item-hd">收件人：</strong>
                                <div class="item-bd">
                                    <input type="text" class="default-text" name="m_receiver" />
                                </div>
                            </li>
                            <li>
                                <strong class="item-hd">收件人电话：</strong>
                                <div class="item-bd">
                                    <input type="text" class="default-text" name="m_phone" />
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
                <!-- 邮寄地址 -->

                <div class="order-info-container mb-50">
                    <div class="order-info-bar"><strong class="bt-bar">订单状态</strong></div>
                    <div class="order-info-block">
                        <ul>

                            <li>
                                <strong class="item-hd">订单状态：</strong>
                                <div class="item-bd">
                                    {loop $statusnames $row}
                                    {if $row['status']!=6 && $row['status']!=0}
                                    <label class="radio-label mr-30"><input type="radio" name="status" {if $row['status']==2}checked{/if} value="{$row['status']}">{$row['name']}</label>
                                    {/if}
                                    {/loop}

                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
                <!-- 支付信息 -->

                <div class="order-amount-bar">
                    <span class="item">原价合计：<strong class="color_f60 org_total_price">&yen;0</strong></span>
                    <span class="item">优惠合计：<strong class="color_f60 privilege_total_price">-&yen;0</strong></span>
                    <span class="item">支付总计：<strong class="color_f60 pay_total_price"><b>&yen;0</b></strong></span>
                    <div class="fr">
                        <a class="btn btn-primary size-L radius ml-5 va-m" id="btn_save" href="javascript:;">保存</a>
                    </div>
                </div>
                <!-- 总计价格 -->
                <input type="hidden" id="member_id" name="member_id" value="0"/>
                <input type="hidden" id="product_id" name="product_id" value="0"/>
                <input type="hidden" id="field_price" name="price" value="0"/>
                <input type="hidden" id="typeid" name="typeid" value="{$typeid}"/>
            </form>
        </td>
    </tr>
</table>
<div id="calendar" style="display: none"></div>
<script>
var isSaving = false;
var typeid = "{$typeid}";
$(function(){

    //选择会员
    $('.choose-member').click(function(){
        CHOOSE.setSome("选择会员",{loadCallback:setMember,maxHeight:525,width:800},SITEURL+'member/dialog_member_list',true);
    });
    $("body").delegate('.delete-member','click',function(){
        $('#member_id').val(0);
        $(this).parent().remove();
    });
    //选择产品
    $('.choose-product').click(function(){
        CHOOSE.setSome("选择产品",{loadCallback:setProduct,maxHeight:500,width:800},SITEURL+'comment/dialog_product_list?typeid='+typeid,true);

    });
    $('body').delegate('.delete-product','click',function(){
        $('#product_id').val(0);
        $(this).parent().remove();
    });

    //选择出发日期
    $('.choose-date').click(function(){
        var suit_id = parseInt($('#suit_list').val());
        if(!suit_id){
            ST.Util.showMsg('请先选择套餐',5,1000);
            return false;
        }else{
            //出发日期选择
            get_calendar(suit_id);
        }

    });

    //数量减少
    $('.sub-btn').click(function(){
        var obj = $(this).parent().find('input');
        var num = parseInt(obj.val());
        if(num>1){
            obj.val(num-1);
            get_total_price();
        }

    });
    //数量增加
    $('.add-btn').click(function(){
        var obj = $(this).parent().find('input');
        var num = parseInt(obj.val());
        obj.val(num+1);
        get_total_price();
    });


    //提交订单
    $('#btn_save').click(function(){
        var mailing_address = $("#suit_list option:selected").attr('data-address');
        if(mailing_address!=1){
            var m_area_prov = $("[name='m_area_prov']").val();
            var m_area_city = $("[name='m_area_city']").val();
            var m_address = $("[name='m_address']").val();
            var m_postcode = $("[name='m_postcode']").val();
            var m_receiver = $("[name='m_receiver']").val();
            var m_phone = $("[name='m_phone']").val();
            if(!m_area_city || !m_area_prov || !m_address || !m_postcode || !m_receiver || !m_phone){
                ST.Util.showMsg('邮寄地址不完整',5,1000);
                return false;
            }
        }
        $('#frm').submit();
    });
    //表单验证

    $("#frm").validate({
        ignore: [],
        focusInvalid:false,
        rules: {
            suitid:
            {
                required:true
            },
            usedate:
            {
                required: true
            },
            linkman:
            {
                required: true
            },
            linktel:
            {
                required:true
            }



        },
        messages: {
            suitid:{
                required:'请选择套餐'
            },
            usedate:
            {
                required:''
            },

            linkman:
            {
                required:"联系人不能为空"
            },
            linktel:
            {
                required:"联系人手机不能为空"
            },
            linkemail:
            {
                required:"联系人邮箱不能为空"
            }
        },
        errUserFunc:function(element){

        },
        submitHandler:function(form){

            var product_id = $("#product_id").val();
            if(product_id==0){
                ST.Util.showMsg('请选择产品',5);
                return false;
            }

            var member_id = $('#member_id').val();
            if(member_id==0){
                ST.Util.showMsg('请选择会员',5);
                return false;
            }

            var total_num = $('#field_dingnum').val();
            if(total_num<=0){
                ST.Util.showMsg('预订人数不能为0',5);
                return false;
            }

            if(!isSaving){
                isSaving = true;
                $.ajax({
                    type:'POST',
                    url:SITEURL+'order/ajax_save_order',
                    data:$('#frm').serialize(),
                    dataType:'json',
                    beforeSend :function(){
                        ST.Util.showMsg('保存中',6,1000000);
                        $('#btn_save').addClass('disabled')
                    },
                    success:function(data){

                        if(data.status){
                            ST.Util.showMsg('订单添加成功',4);
                            setTimeout(function(){
                                location.reload();
                            },1000)

                        }else{
                            var msg = data.msg ? data.msg : '订单添加失败';
                            ST.Util.showMsg(msg, 5);
                            isSaving = false;
                        }
                    },
                    complete: function () {
                        ST.Util.closeBox();
                        $('#btn_save').removeClass('disabled');
                    }
                })
            }
        }
    })
});

/*计算总价*/
function get_total_price()
{
    var dingnum = Number($("#field_dingnum").val());
    var price = $("#field_price").val();
    $(".adult_price_single_txt").html(CURRENCY_SYMBOL+price);

    var total_price = mul(dingnum, price);// adult_num * adult_price;
    $(".adult_price_total_txt").html(CURRENCY_SYMBOL+total_price);

    //总计原价
    var org_total_price = total_price
    var privilege_total_price = 0;
    var pay_total_price = org_total_price-privilege_total_price;

    $('.org_total_price').html(CURRENCY_SYMBOL+org_total_price);
    $('.privilege_total_price').html(CURRENCY_SYMBOL+privilege_total_price);
    $('.pay_total_price').html(CURRENCY_SYMBOL+pay_total_price);
}
function setMember(result,bool)
{

    var html = '<span class="choose-child-item ml-10">'+result.title+'<i class="close-icon delete-member" data-id="'+result.id+'" ></i></span>';
    $("#member_id").val(result.id);
    $(".choose-member").after(html);
    $("#frm").valid();
}
function setProduct(result,bool)
{
    var html = '<span class="choose-child-item ml-10">'+result.title+'<i class="close-icon delete-product" data-id="'+result.id+'" ></i></span>';
    $("#product_id").val(result.id);


    $(".choose-child-item.ml-10").each(function(){
        if($(this).find('.delete-member').length==0)
        {
            $(".choose-child-item.ml-10").remove();
        }
    });

    $(".choose-product").after(html);
    $("#frm").valid();
    get_suit_list();
}

//获取套餐
function get_suit_list(){
    var product_id = $('#product_id').val();
    $.ajax({
        type:'POST',
        url:SITEURL+'order/ajax_suit_list/typeid/'+typeid+'/product_id/'+product_id,
        data:{product_id:product_id},
        dataType:'json',
        success:function(data){
            var obj = $('#suit_list');
            if(data.status){
                obj.empty();
                var html ='<option value="0">请选择套餐</option>';
                $.each(data.list,function(i,row){
                    html += '<option data-address="'+row.mailing_address+'" value="'+row.id+'">'+row.suitname+'</option>';
                });
                obj.append(html);
                suit_list_change_listener();
            }
            else
            {
                $('#suit_list').html('');
            }

            clear_price_date();

        }

    })
}

function suit_list_change_listener()
{
    $("#suit_list").unbind();
    $("#suit_list").change(function(){
        var mailing_address = $("#suit_list option:selected").attr('data-address');
        if(mailing_address != 1){
            $(".mailing_address").show();
        }else{
            $(".mailing_address").hide();
        }
        clear_price_date();
    });
}

function clear_price_date()
{
    $("#usedate").val('');
    $("#field_price").val(0);
    $("#tourer_list_header").html('');
    $("#tourer_list").html('');
    get_total_price();
}

//获取套餐具体报价
function get_suit_price(day,bool){
    var params = {
        suit_id:$('#suit_list').val(),
        useday :day
    };

    $.getJSON(SITEURL+'order/ajax_suit_price',params,function(data){
        var total_price = 0;
        $('#field_dingnum').val(1);
        $('#field_price').val(data.price);
        get_total_price();
        ST.Util.closeDialog();

    })
}
function get_calendar(suit_id,year,month) {
    var width = 500;
    var height = 430;
    var url = SITEURL + 'order/dialog_calendar/suitid/'+suit_id;
    CHOOSE.setSome("选择预订日期",{loadCallback:get_suit_price,maxHeight:height,width:width},url,true);
}

function mul(a, b) {
    var c = 0,
        d = a.toString(),
        e = b.toString();
    try {
        c += d.split(".")[1].length;
    } catch (f) {}
    try {
        c += e.split(".")[1].length;
    } catch (f) {}
    return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
}

</script>
</body>
</html>